/* 
 * Copyright [2018] [Alex/libo(liboware@gmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.sftp.server;


import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.usermanager.impl.BaseUser;

/**
 * @author LiBo/Alex
 * @version V1.0 
 */
public final class FtpServerManager {
    
    /**  
     * @fieldName serverFactory
     * @fieldType FtpServerFactory
     */ 
    private FtpServerFactory serverFactory;
    
    /**  
     * @fieldName serverManager
     * @fieldType FtpServerManager
     */ 
    private static final FtpServerManager serverManager = new FtpServerManager();
    
    /**  
     * @constructor��FtpServerManager  
     * @param serverFactory  
     */ 
    public FtpServerManager(FtpServerFactory serverFactory) {
        super();
        this.serverFactory = serverFactory;
    }
    /**  
     * @constructor��FtpServerManager    
     */ 
    public FtpServerManager() {
        super();
        this.serverFactory = new FtpServerFactory();
    }

    /**  
     * @name serverFactory's getter method
     * @return serverFactory
     */
    public FtpServerFactory getServerFactory() {
        return serverFactory;
    }

    /**  
     * @name serverFactory's setter method
     * @param serverFactory
     * @return void
     */
    public void setServerFactory(FtpServerFactory serverFactory) {
        this.serverFactory = serverFactory;
    }
    
    
    public static FtpServerManager getInstance() {
        return serverManager;
    }
    
    /**  
     * @exception
     */ 
    public void start(FtpServerBean serverBean) {
        try {
            if(serverBean != null) {
                if(serverBean instanceof SimpleFtpServerBean) {
                    SimpleFtpServerBean bean = (SimpleFtpServerBean)serverBean;
                    BaseUser user = new BaseUser();
                    user.setName(serverBean.getAnonymousName());
                    user.setHomeDirectory(serverBean.getFtpServerPath());
                    user.setMaxIdleTime(2000);
                    user.setEnabled(true);
                    serverFactory.getUserManager().save(user);
                    serverFactory.createServer().start();
                }
                else if(serverBean instanceof PermissionFtpServerBean) {
                    
                }
            }
        } catch (FtpException e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        SimpleFtpServerBean bean = new SimpleFtpServerBean();
        bean.setFtpServerPath("D://");
        FtpServerManager.getInstance().start(bean);
    }
}


